package com.liuc.practice.login.repo;

import com.liuc.practice.login.pojo.User;
import com.liuc.practice.login.util.JDBCUtils;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author Liu Chang
 * @Date 2021/9/20 12:17 上午
 */
@Repository
public class UserRepository {

    public User getUser(String name, String password) throws Exception {

        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from user where name=? and password=?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1, name);
        ps.setString(2, password);
        ResultSet rs = ps.executeQuery();

        while (rs.next()) {
            User user = new User();
            user.setId(rs.getString("id"));
            user.setName(rs.getString("name"));
            user.setPassword(rs.getString("password"));
            return user;
        }
        JDBCUtils.close(rs, ps, connection);
        return null;
    }
}
